#!/bin/bash

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
root_dir="$( cd -P "$( dirname "$SOURCE" )"/.. >/dev/null 2>&1 && pwd )"

set -e

notice() {
  echo -e "\033[36m $1 \033[0m "
}

# 大于28.2.1有问题：https://github.com/msojocs/bilibili-linux/issues/170, https://github.com/electron/electron/issues/42519
electron_version="28.2.1"
if [ "$BUILD_ARCH" == "" ];then
  BUILD_ARCH="x64"
elif [ "$BUILD_ARCH" == "amd64" ];then
  BUILD_ARCH="x64"
elif [ "$BUILD_ARCH" == "arm64" ];then
  BUILD_ARCH="arm64"
fi
download_url="https://npmmirror.com/mirrors/electron/${electron_version}/electron-v${electron_version}-linux-${BUILD_ARCH}.zip"
download_url="https://github.com/electron/electron/releases/download/v${electron_version}/electron-v${electron_version}-linux-${BUILD_ARCH}.zip"
if [ "$BUILD_ARCH" == "loong64" ];then
  # 新世界
  electron_version="22.3.27"
  download_url="https://github.com/msojocs/electron-loongarch/releases/download/v${electron_version}/electron-v${electron_version}-linux-loong64.zip"
elif [ "$BUILD_ARCH" == "loongarch64" ];then
  # 旧世界
  electron_version="22.3.27"
  # download_url="http://ftp.loongnix.cn/electron/LoongArch/v22.3.27/electron-v22.3.27-linux-loong64.zip"
  download_url="https://github.com/msojocs/electron-loongarch/releases/download/v${electron_version}/electron-v${electron_version}-linux-loongarch64.zip"
fi

mkdir -p "$root_dir/cache" "$root_dir/tmp"
if [[ ! -f "$root_dir/cache/electron-v${electron_version}-linux-${BUILD_ARCH}.zip" ]];then
  wget -c "$download_url" -O "$root_dir/cache/electron-v${electron_version}-linux-${BUILD_ARCH}.zip.tmp"
  mv "$root_dir/cache/electron-v${electron_version}-linux-${BUILD_ARCH}.zip.tmp" "$root_dir/cache/electron-v${electron_version}-linux-${BUILD_ARCH}.zip"
fi
rm -rf "$root_dir/electron"
mkdir -p "$root_dir/electron"
unzip -o -d "$root_dir/electron" "$root_dir/cache/electron-v${electron_version}-linux-${BUILD_ARCH}.zip"
